<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:lzc="http://schemas.icitadel.com/Lazarus/Compiled.xsd" version="1.0">

	<xsl:output method="text"/>

	<xsl:include href="../../common.xsl" />
	<xsl:include href="../../convert.xsl" />

   <xsl:template match="lzc:project">
	
	<!-- Create the Constraints -->
	<xsl:text>-- Constraint Creation
</xsl:text>
	<xsl:for-each select="lzc:tables/lzc:table[ count( lzc:fields/lzc:field/lzc:constraints/lzc:constraint ) &gt; 0 ]">
		<xsl:variable name="TABLE_NAME">tbl<xsl:value-of select="@name" /></xsl:variable>
		<xsl:for-each select=".//lzc:constraint">
			<xsl:text>ALTER TABLE [</xsl:text>
			<xsl:value-of select="$TABLE_NAME" />
			<xsl:text>] ADD CONSTRAINT [</xsl:text>
			<xsl:value-of select="@name" />
			<xsl:text>] CHECK ( </xsl:text>
			<xsl:call-template name="CheckConstraint">
				<xsl:with-param name="pConstraint" select="." />
				<xsl:with-param name="pField" select="ancestor::lzc:field" />
			</xsl:call-template>
			<xsl:text> )
</xsl:text>
		</xsl:for-each>
<xsl:text>
</xsl:text>
   </xsl:for-each>
   
   </xsl:template>

	<xsl:template name="CheckConstraint">
		<xsl:param name="pConstraint" />
		<xsl:param name="pField" />
		<xsl:choose>
			<xsl:when test="$pConstraint/@operation = $OPERATIONTYPE_EQUAL">
				<xsl:call-template name="SimpleCheckConstraint">
					<xsl:with-param name="pFieldName" select="$pField/@name" />
					<xsl:with-param name="pDataType" select="$pField/@type" />
					<xsl:with-param name="pOperator">
						<xsl:choose>
							<xsl:when test="$pConstraint/@modifier = $MODIFIERTYPE_NOT">
								<xsl:text>&lt;&gt;</xsl:text>
							</xsl:when>
							<xsl:otherwise>
								<xsl:text>=</xsl:text>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:with-param>
					<xsl:with-param name="pValue" select="$pConstraint/@value" />
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="$pConstraint/@operation = $OPERATIONTYPE_GREATERTHAN">
				<xsl:call-template name="SimpleCheckConstraint">
					<xsl:with-param name="pFieldName" select="$pField/@name" />
					<xsl:with-param name="pDataType" select="$pField/@type" />
					<xsl:with-param name="pOperator">
						<xsl:choose>
							<xsl:when test="$pConstraint/@modifier = $MODIFIERTYPE_NOT">
								<xsl:text>&lt;=</xsl:text>
							</xsl:when>
							<xsl:otherwise>
								<xsl:text>&gt;</xsl:text>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:with-param>
					<xsl:with-param name="pValue" select="$pConstraint/@min-value" />
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="$pConstraint/@operation = $OPERATIONTYPE_GREATERTHANOREQUAL">
				<xsl:call-template name="SimpleCheckConstraint">
					<xsl:with-param name="pFieldName" select="$pField/@name" />
					<xsl:with-param name="pDataType" select="$pField/@type" />
					<xsl:with-param name="pOperator">
						<xsl:choose>
							<xsl:when test="$pConstraint/@modifier = $MODIFIERTYPE_NOT">
								<xsl:text>&lt;</xsl:text>
							</xsl:when>
							<xsl:otherwise>
								<xsl:text>&gt;=</xsl:text>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:with-param>
					<xsl:with-param name="pValue" select="$pConstraint/@min-value" />
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="$pConstraint/@operation = $OPERATIONTYPE_LESSTHAN">
				<xsl:call-template name="SimpleCheckConstraint">
					<xsl:with-param name="pFieldName" select="$pField/@name" />
					<xsl:with-param name="pDataType" select="$pField/@type" />
					<xsl:with-param name="pOperator">
						<xsl:choose>
							<xsl:when test="$pConstraint/@modifier = $MODIFIERTYPE_NOT">
								<xsl:text>&gt;=</xsl:text>
							</xsl:when>
							<xsl:otherwise>
								<xsl:text>&lt;</xsl:text>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:with-param>
					<xsl:with-param name="pValue" select="$pConstraint/@max-value" />
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="$pConstraint/@operation = $OPERATIONTYPE_LESSTHANOREQUAL">
				<xsl:call-template name="SimpleCheckConstraint">
					<xsl:with-param name="pFieldName" select="$pField/@name" />
					<xsl:with-param name="pDataType" select="$pField/@type" />
					<xsl:with-param name="pOperator">
						<xsl:choose>
							<xsl:when test="$pConstraint/@modifier = $MODIFIERTYPE_NOT">
								<xsl:text>&gt;</xsl:text>
							</xsl:when>
							<xsl:otherwise>
								<xsl:text>&lt;=</xsl:text>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:with-param>
					<xsl:with-param name="pValue" select="$pConstraint/@max-value" />
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="$pConstraint/@operation = $OPERATIONTYPE_INCLUSIVEBETWEEN">
				<xsl:choose>
					<xsl:when test="$pConstraint/@modifier = $MODIFIERTYPE_NOT">
						<xsl:text>[</xsl:text>
						<xsl:value-of select="$pField/@name" />
						<xsl:text>] &lt; </xsl:text>
						<xsl:call-template name="getSQLValue">
							<xsl:with-param name="pDataType" select="$pField/@type" />
							<xsl:with-param name="pValue" select="$pConstraint/@min-value" />
						</xsl:call-template>
						<xsl:text> AND </xsl:text>
						<xsl:text>[</xsl:text>
						<xsl:value-of select="$pField/@name" />
						<xsl:text>] &gt; </xsl:text>
						<xsl:call-template name="getSQLValue">
							<xsl:with-param name="pDataType" select="$pField/@type" />
							<xsl:with-param name="pValue" select="$pConstraint/@max-value" />
						</xsl:call-template>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>[</xsl:text>
						<xsl:value-of select="$pField/@name" />
						<xsl:text>] BETWEEN </xsl:text>
						<xsl:call-template name="getSQLValue">
							<xsl:with-param name="pDataType" select="$pField/@type" />
							<xsl:with-param name="pValue" select="$pConstraint/@min-value" />
						</xsl:call-template>
						<xsl:text> AND </xsl:text>
						<xsl:call-template name="getSQLValue">
							<xsl:with-param name="pDataType" select="$pField/@type" />
							<xsl:with-param name="pValue" select="$pConstraint/@max-value" />
						</xsl:call-template>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="$pConstraint/@operation = $OPERATIONTYPE_EXCLUSIVEBETWEEN">
				<xsl:choose>
					<xsl:when test="$pConstraint/@modifier = $MODIFIERTYPE_NOT">
						<xsl:text>[</xsl:text>
						<xsl:value-of select="$pField/@name" />
						<xsl:text>] &lt;= </xsl:text>
						<xsl:call-template name="getSQLValue">
							<xsl:with-param name="pDataType" select="$pField/@type" />
							<xsl:with-param name="pValue" select="$pConstraint/@min-value" />
						</xsl:call-template>
						<xsl:text> AND </xsl:text>
						<xsl:text>[</xsl:text>
						<xsl:value-of select="$pField/@name" />
						<xsl:text>] &gt;= </xsl:text>
						<xsl:call-template name="getSQLValue">
							<xsl:with-param name="pDataType" select="$pField/@type" />
							<xsl:with-param name="pValue" select="$pConstraint/@max-value" />
						</xsl:call-template>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>[</xsl:text>
						<xsl:value-of select="$pField/@name" />
						<xsl:text>] &gt; </xsl:text>
						<xsl:call-template name="getSQLValue">
							<xsl:with-param name="pDataType" select="$pField/@type" />
							<xsl:with-param name="pValue" select="$pConstraint/@min-value" />
						</xsl:call-template>
						<xsl:text> AND </xsl:text>
						<xsl:text>[</xsl:text>
						<xsl:value-of select="$pField/@name" />
						<xsl:text>] &lt; </xsl:text>
						<xsl:call-template name="getSQLValue">
							<xsl:with-param name="pDataType" select="$pField/@type" />
							<xsl:with-param name="pValue" select="$pConstraint/@max-value" />
						</xsl:call-template>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="$pConstraint/@operation = $OPERATIONTYPE_IN">
				<xsl:text>[</xsl:text>
				<xsl:value-of select="$pField/@name" />
				<xsl:text>]</xsl:text>
				<xsl:if test="$pConstraint/@modifier = $MODIFIERTYPE_NOT">
					<xsl:text> NOT</xsl:text>
				</xsl:if>
				<xsl:text> IN ( </xsl:text>
				<xsl:for-each select="lzc:constraint-values/lzc:constraint-value">
					<xsl:if test="position() &gt; 1">
						<xsl:text>, </xsl:text>
					</xsl:if>
					<xsl:call-template name="getSQLValue">
						<xsl:with-param name="pDataType" select="$pField/@type" />
						<xsl:with-param name="pValue" select="@value" />
					</xsl:call-template>
				</xsl:for-each>
				<xsl:text> )</xsl:text>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template name="SimpleCheckConstraint">
		<xsl:param name="pFieldName" />
		<xsl:param name="pDataType" />
		<xsl:param name="pOperator" />
		<xsl:param name="pValue" />

		<xsl:text>[</xsl:text>
		<xsl:value-of select="$pFieldName" />
		<xsl:text>] </xsl:text>
		<xsl:value-of select="$pOperator" />
		<xsl:text> </xsl:text>
		<xsl:call-template name="getSQLValue">
			<xsl:with-param name="pDataType" select="$pDataType" />
			<xsl:with-param name="pValue" select="$pValue" />
		</xsl:call-template>
		
	</xsl:template>
	
</xsl:stylesheet>
  
<!--
OPERATIONTYPE_GREATERTHAN			
OPERATIONTYPE_GREATERTHANOREQUAL	
OPERATIONTYPE_LESSTHAN				
OPERATIONTYPE_LESSTHANOREQUAL		
OPERATIONTYPE_EQUAL					
OPERATIONTYPE_INCLUSIVEBETWEEN	
OPERATIONTYPE_EXCLUSIVEBETWEEN	
OPERATIONTYPE_IN						
-->